﻿using MediatR;
using SqliteApply.Common.Infrastructure.Interface;
using SqliteApply.Common.Table;
using SqliteApply.Enum;

namespace SqliteApply.Addlication.SqliteW
{
    public class LoginSearchHandler : IRequestHandler<LoginSearchCommon, Result>
    {
        public readonly IBaseRepository<Login> LoginRepository;

        public LoginSearchHandler(IBaseRepository<Login> loginRepository)
        {
            LoginRepository = loginRepository;
        }

        public async Task<Result> Handle(LoginSearchCommon request, CancellationToken cancellationToken)
        {
            var res = new Result() { code = 1, msg = "查询失败" };
            try
            {

                var list = LoginRepository.GetAll().ToList();
                if (request.id != -1)
                {
                    list = list.Where(x => x.id == request.id).ToList();
                    if (list.Count > 0)
                    {
                        res.code = 0;
                        res.msg = "查询成功";
                        res.data = list;
                    }
                    else
                    {
                        res.code = 1;
                        res.msg = "查询失败";
                    }
                   
                }
                res.data = list;
            }
            catch (Exception)
            {

                throw;
            }
            return res;
        }
    }
}
